<?php 
header("Content-type: text/html; charset=utf-8"); 
/*
 * 	config.php
 * 	配置文件
 */
if (true){
	$ulist = array("renjm","futx","liql","chengmq","lil","lixm");
	$ulist = array("renjm","futx","liql","lil","lixm");
	$qlist = array(
		'汤圆|丸子',
		'哈密瓜|西瓜',
		'包子|水饺',
		'汉堡包|肉夹馍',
		'宫锁心玉|宫锁珠帘',
		'步步惊心|宫锁心玉',
		'钢笔|中性笔',
		'玫瑰|月季',
		'董永|许仙',
		'若曦|晴川',
		'谢娜|李湘',
		'孟非|乐嘉',
		'牛奶|豆浆',
		'保安|保镖',
		'白菜|生菜',
		'辣椒|芥末',
		'赵敏|黄蓉',
		'海豚|海狮',
		'水盆|水桶',
		'唇膏|口红',
		'森马|以纯',
		'烤肉|涮肉',
		'气泡|水泡',
		'纸巾|手帕',
		'杭州|苏州',
		'香港|台湾',
		'首尔|东京',
		'橙子|橘子',
		'葡萄|提子',
		'太监|人妖',
		'蝴蝶|蜜蜂',
		'小品|话剧',
		'裸婚|闪婚',
		'新年|跨年',
		'吉他|琵琶',
		'公交|地铁',
		'剩女|御姐',
		'童话|神话',
		'作家|编剧',
		'警察|捕快',
		'结婚|订婚',
		'奖牌|金牌',
		'孟飞|乐嘉',
		'那英|韩红',
		'面包|蛋糕',
		'作文|论文',
		'油条|麻花',
		'壁纸|贴画',
		'金庸|古龙',
		'枕头|抱枕',
		'手机|座机',
		'同学|同桌',
		'婚纱|喜服',
		'老佛爷|老天爷',
		'魔术师|魔法师',
		'鸭舌帽|遮阳帽',
		'双胞胎|龙凤胎',
		'情人节|光棍节',
		'丑小鸭|灰姑娘',
		'富二代|高富帅',
		'生活费|零花钱',
		'麦克风|扩音器',
		'郭德纲|周立波',
		'图书馆|图书店',
		'男朋友|前男友',
		'洗衣粉|皂角粉',
		'牛肉干|猪肉脯',
		'泡泡糖|棒棒糖',
		'小沈阳|宋小宝',
		'土豆粉|酸辣粉',
		'蜘蛛侠|蝙蝠侠',
		'口香糖|木糖醇',
		'酸菜鱼|水煮鱼',
		'小笼包|灌汤包',
		'薰衣草|满天星',
		'张韶涵|王心凌',
		'刘诗诗|刘亦菲',
		'甄嬛传|红楼梦',
		'甄子丹|李连杰',
		'包青天|狄仁杰',
		'大白兔|金丝猴',
		'果粒橙|鲜橙多',
		'沐浴露|沐浴盐',
		'洗发露|护发素',
		'自行车|电动车',
		'班主任|辅导员',
		'过山车|碰碰车',
		'铁观音|碧螺春',
		'十面埋伏|四面楚歌',
		'成吉思汗|努尔哈赤',
		'谢娜张杰|邓超孙俪',
		'福尔摩斯|工藤新一',
		'贵妃醉酒|黛玉葬花',
		'流星花园|花样男子',
		'神雕侠侣|天龙八部',
		'天天向上|非诚勿扰',
		'勇往直前|全力以赴',
		'鱼香肉丝|四喜丸子',
		'麻婆豆腐|皮蛋豆腐',
		'语无伦次|词不达意',
		'鼠目寸光|井底之蛙',
		'近视眼镜|隐形眼镜',
		'美人心计|倾世皇妃',
		'夏家三千金|爱情睡醒了',
		'降龙十八掌|九阴白骨爪',
		'红烧牛肉面|香辣牛肉面',
		'江南style|最炫民族风',
		'梁山伯与祝英台|罗密欧与朱丽叶'
	);
	
	$txt_path = "txt";
	$ut = "tiket";
	
	$cuser = isset($_COOKIE["U"]) ? $_COOKIE["U"] : '';
	
	#存放目录
	if (!is_dir($txt_path)){
		mkdir($txt_path);
	}
	
	#初始化游戏
	if (!empty($cuser)){
		#tiket存储路径
		$tiket_path = $txt_path."/".$ut;
		$tiket = "";
		
		#初始化tiket
		if (!is_file($tiket_path)){
			$tiket = date("YmdHis", time()).rand(1000,9999);
			$f = fopen($tiket_path, "w");
			fwrite($f, $tiket);
			fclose($f);
		}else{
			$tiket =  file_get_contents($tiket_path);
		}
		
		#tiket文件夹
		$tiket_dir_path = "";
		if (!empty($tiket)){
			$tiket_dir_path= $txt_path."/".$tiket;
			if (!is_dir($tiket_dir_path)){
				mkdir($tiket_dir_path);
			}
		}
		
		#开始游戏
		$udata_path = $tiket_dir_path."/udata";
		
		if (!is_file($udata_path)){
			$udata = array();
			$cid = rand(0, count($qlist)-1);
			shuffle($ulist);
			$uid = $ulist[rand(0, count($ulist)-1)];
			for ($i=0;$i<count($ulist);$i++){
				#用户列表
				$udata["ulist"] = $ulist;
				#卧底
				$udata["uid"] = $uid;
				#关键词
				$udata["cid"] = $cid;
				#阶段
				$udata["status"] = 1;
				#发言人
				$udata["speak"] = 0;
			}
			$f = fopen($udata_path, "w");
			fwrite($f, json_encode($udata));
			fclose($f);
		}else{
			$jsonStr =  file_get_contents($udata_path);
			$udata = json_decode($jsonStr, true);
		}
	
		#关键词
		$qs = explode("|", $qlist[$udata["cid"]]);
	    $gjc = ($cuser==$udata["uid"]) ? $qs[0] : $qs[1];
		
		#聊天内容
		$chart = "";
		$chart_path = $tiket_dir_path."/chart";
		if (!is_file($chart_path)){
			$f = fopen($chart_path, "w");
			fwrite($f, $chart);
			fclose($f);
		}else{
			$chart =  file_get_contents($chart_path);
		}
	
		#票数
		$toup_path = $tiket_dir_path."/toup";
		$toup = array();
		foreach ($udata["ulist"] as $u){
			$toup["ps"][$u] = 0;
		}
		$toup["us"] = array();
		if (!is_file($toup_path)){
			$f = fopen($toup_path, "w");
			fwrite($f, json_encode($toup));
			fclose($f);
		}else{
			$jsonStr =  file_get_contents($toup_path);
			$toup = json_decode($jsonStr, true);
		}
		
		#当前状态
		if ($udata["status"]=="1"){
		
			#判断游戏是否结束
		    $gameover = false;
		    if (count($udata["ulist"])<=3){
		    	$gameover = true;
		    }
		    if (!in_array($udata["uid"], $udata["ulist"])){
		    	$gameover = true;
		    }		
		    if ($gameover){
		    	if (in_array($udata["uid"], $udata["ulist"])){
		    		$uwin = $udata["uid"];
		    	}else{
		    		$tmp = array();
		    		foreach ($ulist as $u){
		    			if ($u != $udata["uid"]){
		    				$tmp[] = $u;
		    			}
		    		}
		    		$uwin = implode(",", $tmp);
		    	}
		    	$status = "游戏结束! 胜利者是:{$uwin}";
		    }else{
				$status = "当前状态:请等待 ".$udata["ulist"][$udata["speak"]]." 发言";
		    }
		}elseif ($udata["status"]=="2"){
			$toupstr = "";
			foreach ($toup["ps"] as $a=>$b){
				$toupstr .= "  <a href=\"javascript:toup('".$a."');\">".$a."</a>(".$b.")  ";
			}
			$status = "当前状态:请投票 {".$toupstr."}";
		}
	}
}
#输出 $tiket

/*
 *  操作动作
 * 
 */

$do = isset($_GET["do"]) ? $_GET["do"] : '';
if ($do=="login"){
	$u = $_POST["name"];
	if (in_array($u, $ulist)){
		setcookie("U",$u, time()+3600*24);
		header('Location: index.php');
	}
}elseif ($do=="loginout"){
	setcookie("U","", time()+3600*24);
	header('Location: index.php');
}elseif ($do=="ajax"){
	$code = 1;
	if (empty($cuser)){
		$code = 0;
		$msg = "NO LOGIN";
	}
	if (empty($tiket_dir_path)){
		$code = 0;
		$msg = "NO TIKET";
	}
	$msg = "SCUESS";
	$data = array();
	if ($code==1){
		$m = $_GET["m"];
		if ($m=="status"){
			#取得信息与状态
			$data = array("CHART"=>$chart,"STATUS"=>$status,"GJC"=>$gjc);
		}elseif ($m=="speak"){
			#发言
			if (in_array($cuser, $udata["ulist"])){
				if ($cuser==$udata["ulist"][$udata["speak"]]){
					$v = $cuser.":".$_GET["v"]."<br />";
					$f = fopen($chart_path, "w");
					fwrite($f, $v.$chart);
					fclose($f);
					$udata["speak"] += 1;
					if (count($udata["ulist"])-$udata["speak"]<1){
						$udata["status"] = 2;
					}
					$f = fopen($udata_path, "w");
					fwrite($f, json_encode($udata));
					fclose($f);
				}else{
					$code = 0;
					$msg = "还没到你发言,别着急!";
				}
			}else{
				$code = 0;
				$msg = "死人无法发言!";
			}
		}elseif ($m=="toup"){
			#投票
			if (in_array($cuser, $udata["ulist"])){
				if (!in_array($cuser,$toup["us"])){
					$v = $_GET["v"];
					$toup["us"][] = $cuser;
					$toup["ps"][$v] += 1;
					
					$strv = $cuser."投票给".$v;
					$f = fopen($chart_path, "w");
					fwrite($f, $strv.$chart);
					fclose($f);
					
					$f = fopen($toup_path, "w");
					fwrite($f, json_encode($toup));
					fclose($f);
					
					if (count($toup["us"])==count($udata["ulist"])){
						$sr = "";
						#剔除死人
						arsort($toup["ps"]);
						$tmp1 = each($toup["ps"]);
						$tmp2 = each($toup["ps"]);
						if ($tmp1["value"]!=$tmp2["value"]){
							$tmpd = array();
							foreach ($udata["ulist"] as $v){
								if ($v==$tmp1["key"]){
									$sr = $v;
								}else{
									$tmpd[] = $v;
								}
							}
							$udata["ulist"] = $tmpd;
						}
						
						$udata["status"] = 1;
						$udata["speak"] = 0;
						
						$f = fopen($udata_path, "w");
						fwrite($f, json_encode($udata));
						fclose($f);
						
						if (!empty($sr)){
							$v = $sr."已阵亡<br />";
						}else{
							$v = "平安夜你们继续<br />";
						}
						$f = fopen($chart_path, "w");
						fwrite($f, $v.$chart);
						fclose($f);
						
						unlink($toup_path);
					}
					
				}else{
					$code = 0;
					$msg = "请不要重复投票!";
				}
			}else{
				$code = 0;
				$msg = "死人无法投票!";
			}
		}
	}
	$res = array("CODE"=>$code,"MSG"=>$msg);
	if (!empty($data)){
		$res["DATA"] = $data;
	}
	echo json_encode($res);
	exit();
}elseif ($do=="debug_clear"){
	function deldir($dir) {
		//先删除目录下的文件：
		$dh=opendir($dir);
		while ($file=readdir($dh)) {
			if($file!="." && $file!="..") {
				$fullpath=$dir."/".$file;
				if(!is_dir($fullpath)) {
					unlink($fullpath);
				} else {
					deldir($fullpath);
				}
			}
		}
	  
		closedir($dh);
		//删除当前文件夹：
		if(rmdir($dir)) {
			return true;
		} else {
			return false;
		}
	}
	deldir($txt_path);
	header('Location: index.php');
}elseif ($do=="restart"){
	unlink($tiket_path);
	header('Location: index.php');
}

/*
 * 	显示页面
 */
if (empty($cuser)){
	echo <<<EOD
	<form action="?do=login" method="post" name="login">
		<input type="text" name="name" value="" />
		<input type="submit" name="submit" value="LOGIN"/>
	</form>
EOD;
	exit();
}else{	
	
	if ($cuser=="renjm"){
	echo <<<EOD
	<a href="?do=restart">重新开始游戏</a>   <a href="?do=debug_clear">清除所有数据</a><hr />
EOD;
	}
    
	echo <<<EOD
	<a href="?do=loginout">{$cuser}</a><hr />
	你的关键词是:<div id="gjc">{$gjc}</div>
	<hr>
	<div id="status">{$status}</div>
	<hr>
	<form name="Frm" action="" method="post" onsubmit="return send();">
	<input id="chart" type="txt" name="chart" size="60" value="" />
	<input type="submit" name="subchart" value="SEND" />
	</form>
	<hr>
	<div id="charts">{$chart}</div>
	<hr>
EOD;
}
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
var toup;
var send;
$(function(){
	var timer;
	var getChart = function (){
		$.ajax({ 
			type: "GET", 
			dataType: "json",
			url: "index.php?do=ajax&m=status", 
			success: function(d){ 
				if (d["CODE"]=="1"){
					if (d["DATA"]["CHART"]!=$("#charts").html()){
						$("#charts").html(d["DATA"]["CHART"]);
					}
					if (d["DATA"]["STATUS"]!=$("#status").html()){
						$("#status").html(d["DATA"]["STATUS"]);
					}
					if (d["DATA"]["GJC"]!=$("#gjc").html()){
						$("#gjc").html(d["DATA"]["GJC"]);
					}
				}
			}
		});
	}

	toup = function (v){
		$.ajax({ 
			type: "GET", 
			dataType: "json",
			url: "index.php?do=ajax&m=toup&v="+encodeURI(v), 
			success: function(d){ 
				if (d["CODE"]=="1"){
					getChart();
				}else{
					alert(d["MSG"]);
				}
			}
		});
	}

	send = function (){
		var v = $("#chart").val();
		if (v){
			$("#chart").val("");
			$.ajax({ 
				type: "GET", 
				dataType: "json",
				url: "index.php?do=ajax&m=speak&v="+encodeURI(v), 
				success: function(d){ 
					if (d["CODE"]=="1"){
						getChart();
					}else{
						alert(d["MSG"]);
					}
				}
			});
		}else{
			alert("输入内容!");
		}
		return false;
	}
	timer = setInterval(getChart,1000);
});
</script>